package com.wego.controller;

import com.wego.bean.IdName;
import com.wego.bean.StateBean;
import com.wego.bean.TableBean;
import com.wego.converter.CategoryConverter;
import com.wego.entity.domain.Category;
import com.wego.entity.domain.Goods;
import com.wego.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @className: CategoryController
 * @description: TODO 类描述
 * @author: Kuangkeli
 * @date: 2023/07/13 8:22
 **/
@Controller
@RequestMapping("manager/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryConverter categoryConverter;

    @GetMapping("list_id_name")
    public @ResponseBody List<IdName> list_Id_Name() {
        final List<Category> categoryList = categoryService.selectCategoryByPid(null);
        return categoryConverter.categoryList2IdNameList(categoryList);
    }

    @GetMapping("categoryParentList")
    public @ResponseBody TableBean<Category> categoryParentList() {
        final List<Category> categoryList = categoryService.selectList(null);
        TableBean<Category> tableBean = new TableBean<>();
        tableBean.setCode(0);
        tableBean.setMsg("");
        tableBean.setCount(categoryService.getCounts());
        tableBean.setData(categoryList);
        return tableBean;
    }


    @GetMapping("selectIdNameByPid")
    public @ResponseBody List<IdName> selectIdNameByPid(Long pid) {
        final List<Category> categoryList = categoryService.selectCategoryByPid(pid);
        return categoryConverter.categoryList2IdNameList(categoryList);
    }

    @GetMapping("/openAddUpdate")
    public String openAddUpdate(Long id, Model model, Long addtypes) {
        if (id != null) {
            Category category = categoryService.selectByPrimaryKey(id);
            model.addAttribute("category", category);
            if (category.getPid() == null) {
                addtypes = 0L;
            }
        }
        model.addAttribute("addtypes", addtypes);
        return "manager/category_add_update";
    }

    @PostMapping("/addUpdate")
    public @ResponseBody StateBean addUpdate(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        boolean res;
        String msg;
        if(category.getInfo() != null && category.getInfo().equals("")){
            category.setInfo(null);
        }
        Long id = category.getId();
        if (id != null) {
            //修改
            res = categoryService.updateByPrimaryKeySelective(category);
            msg = res ? "修改成功！" : "修改失败";
        } else {
            //添加
            category.setCreateTime(LocalDateTime.now());
            res = categoryService.insertSelective(category);
            msg = res ? "添加成功！" : "添加失败";
        }
        return StateBean.builder()
                .code(200)
                .message("成功")
                .build();
    }


}
